package com.hngy.xpq.android_notebook;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.hngy.xpq.android_notebook.adapter.NotebookAdapter;
import com.hngy.xpq.android_notebook.bean.Notepbook;
import com.hngy.xpq.android_notebook.database.SQLiteHelper;

import java.util.List;


public class NotepadActivity extends Activity {
    ListView listView;
    List<Notepbook> list,list1,list2;
    SQLiteHelper mSQLiteHelper;
    NotebookAdapter adapter;
    EditText query;
    Button bin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notepad);

        query = findViewById(R.id.ed_query);
        bin= findViewById(R.id.bin_query);

        listView = (ListView) findViewById(R.id.listview);
        ImageView add = (ImageView) findViewById(R.id.add);
        initData("query");
        list = list2;
        bin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(query.getText())){
                    initData("query");
                } else {
                    initData("query_text");
                }
            }


        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NotepadActivity.this,
                        RecordActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    protected void initData(String a) {
        mSQLiteHelper= new SQLiteHelper(this);
        if (a.equals("query")){
            showQueryData();
            list = list2;
        } else if (a.equals("query_text")){
            bodyQueryData();
            list = list1;
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id){
                Notepbook notepadBean = list.get(position);
                Intent intent = new Intent(NotepadActivity.this, RecordActivity.class);
                intent.putExtra("id", notepadBean.getId());
                intent.putExtra("time", notepadBean.getNotepadTime()); //记录的时间
                intent.putExtra("content", notepadBean.getNotepadContent()); //记录的内容
                NotepadActivity.this.startActivityForResult(intent, 1);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int
                    position, long id) {
                AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder( NotepadActivity.this)
                        .setMessage("是否删除此事件？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Notepbook notepadBean = list.get(position);
                                if(mSQLiteHelper.deleteData(notepadBean.getId())){
                                    list.remove(position);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(NotepadActivity.this,"删除成功",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                dialog =  builder.create();
                dialog.show();
                showQueryData();
                list = list2;
                return true;
            }
        });
    }
    private void showQueryData(){
        if (list2 != null){
            list2.clear();
        }
        list2 = mSQLiteHelper.query();
        adapter = new NotebookAdapter(this, list2);
        listView.setAdapter(adapter);
    }

    private void bodyQueryData() {
        if ( list1 != null ){
            list1.clear();
        } else {
            String query_text = query.getText().toString();
            list1 = mSQLiteHelper.query2(query_text);
            adapter = new NotebookAdapter(this,list1);
            listView.setAdapter(adapter);
            query.setText("");
        }
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            showQueryData();
        }
    }
}
